cmake_minimum_required(VERSION 3.5)
set(PROJECT_NAME_D "AutomaticSorterDemo")

project(${PROJECT_NAME_D} VERSION 0.1 LANGUAGES CXX C)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)


# 添加 C 和 C++ 支持
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "-pthread")
# add_subdirectory(rknn-cpp-Multithreading)
add_definitions(-DCL_TARGET_OPENCL_VERSION=200)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
# 查找 gpiod 包
find_package(PkgConfig REQUIRED)
pkg_check_modules(GPIOD REQUIRED libgpiodcxx)
# rknn-toolkit
set(RKNN_MT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/rknn-cpp-Multithreading)

# rknn api
# set(RKNN_API_PATH ${RKNN_MT_SOURCE_DIR}/../../runtime/RK3588/${CMAKE_SYSTEM_NAME}/librknn_api)
set(LIB_ARCH aarch64)
set(RKNN_RT_LIB ${RKNN_MT_SOURCE_DIR}/include/librknnrt.so)

#rga
set(RGA_PATH ${RKNN_MT_SOURCE_DIR}/include/3rdparty/rga/RK3588)
set(RGA_LIB ${RGA_PATH}/lib/Linux/${LIB_ARCH}/librga.so)


include_directories(${RKNN_API_PATH}/include)
include_directories(${RKNN_MT_SOURCE_DIR}/include/3rdparty)
include_directories(${RGA_PATH}/include)
# rknn_yolov5_demo
include_directories( ${RKNN_MT_SOURCE_DIR}/include)

include_directories(${GPIOD_INCLUDE_DIRS})

# opencv
find_package(OpenCV REQUIRED)

# opencl
find_package(OpenCL REQUIRED)

MESSAGE(STATUS "OpenCL library status:")
MESSAGE(STATUS "    version: ${OpenCL_VERSION_STRING}")
MESSAGE(STATUS "    libraries: ${OpenCL_LIBRARY}")
MESSAGE(STATUS "    include path: ${OpenCL_INCLUDE_DIR}")

set(PROJECT_SOURCES
        main.cpp
        driver.cpp
        driver.h
        StateMachine.hpp
        mainwindow.cpp
        V4L2CamCTL.cpp
        V4L2CamCTL.h
        mainwindow.h
        ImageProcessing.hpp
        mainwindow.ui
        ${RKNN_MT_SOURCE_DIR}/src/postprocess.cc
        ${RKNN_MT_SOURCE_DIR}/src/preprocess.cc
        ${RKNN_MT_SOURCE_DIR}/src/rkYolov8_seg.cc
        ${RKNN_MT_SOURCE_DIR}/include/opencl_accelerate.hpp
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME_D}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET untitled APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(${PROJECT_NAME_D} SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(${PROJECT_NAME_D}
            ${PROJECT_SOURCES}
            rknn_yolovX.h rknn_yolovX.cpp
        )
    endif()
endif()
# find_library(LIBRARY_NAME .so HINTS /home/shenao/untitled/rknn-cpp-Multithreading/include)
# execute_process(COMMAND LIBRARY_NAME /home/shenao/untitled/rknn-cpp-Multithreading/include -name "*.so" OUTPUT_VARIABLE SO_FILES)
execute_process(
  COMMAND find ${CMAKE_SOURCE_DIR}/rknn-cpp-Multithreading/include -name "*.so"
  OUTPUT_VARIABLE SO_FILES
  OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "SO_FILES: ${SO_FILES}")
target_link_libraries(${PROJECT_NAME_D} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
    ${OpenCV_LIBS}
    ${RGA_LIB}
    ${RKNN_RT_LIB}
    OpenCL::OpenCL
    ${GPIOD_LIBRARIES}
    # ${SO_FILES}
    )
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.untitled)
endif()
set_target_properties(${PROJECT_NAME_D} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME_D}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME_D})
endif()
